package com.ruoyi.tdqq.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.system.api.RemoteDeptService;
import com.ruoyi.system.api.domain.SysDept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.tdqq.domain.TdqqNh;
import com.ruoyi.tdqq.service.ITdqqNhService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.client.RestTemplate;

/**
 * 农户信息Controller
 * 
 * @author zlx
 * @date 2025-09-09
 */
@RestController
@RequestMapping("/nh")
public class TdqqNhController extends BaseController
{
    @Autowired
    private ITdqqNhService tdqqNhService;
    @Autowired
    private RemoteDeptService remoteDeptService;

    /*@Autowired
    private RestTemplate restTemplate;

    // 新增restTemplate对象注入方法，注意，此处LoadBalanced注解一定要加上，否则无法远程调用
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate()
    {
        return new RestTemplate();
    }

    @GetMapping("/user")
    public String get()
    {
        return restTemplate.getForObject("http://ruoyi-system/user/info/admin", String.class);
    }*/
    /**
     * 查询农户信息列表
     */
    @RequiresPermissions("tdqq:nh:list")
    @GetMapping("/list")
    public TableDataInfo list(TdqqNh tdqqNh)
    {
        startPage();
        SysDept sysDept= remoteDeptService.getInfo(tdqqNh.getDeptId(), SecurityConstants.INNER).getData();
        List<SysDept> deptList = remoteDeptService.getDeptChildrenList(tdqqNh.getDeptId(), SecurityConstants.INNER).getData();
        deptList.add(sysDept);
        Map<Long, String> map = deptList.stream().collect(Collectors.toMap(SysDept::getDeptId, SysDept::getDeptName));
        List<Long> tempList = deptList.stream().map(SysDept::getDeptId).collect(Collectors.toList());
        tdqqNh.setDeptIds(tempList);
        tdqqNh.setDeptId(null);
        List<TdqqNh> list = tdqqNhService.selectTdqqNhList(tdqqNh);
        list.forEach(tdqqNh1 -> {tdqqNh1.setDeptName(map.get(tdqqNh1.getDeptId()));});
        return getDataTable(list);
    }

    /**
     * 导出农户信息列表
     */
    @RequiresPermissions("tdqq:nh:export")
    @Log(title = "农户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TdqqNh tdqqNh)
    {
        List<TdqqNh> list = tdqqNhService.selectTdqqNhList(tdqqNh);
        ExcelUtil<TdqqNh> util = new ExcelUtil<TdqqNh>(TdqqNh.class);
        util.exportExcel(response, list, "农户信息数据");
    }

    /**
     * 获取农户信息详细信息
     */
    @RequiresPermissions("tdqq:nh:query")
    @GetMapping(value = "/{nhId}")
    public AjaxResult getInfo(@PathVariable("nhId") Long nhId)
    {
        System.out.println(tdqqNhService.selectTdqqNhByNhId(nhId));
        return success(tdqqNhService.selectTdqqNhByNhId(nhId));
    }

    /**
     * 新增农户信息
     */
    @RequiresPermissions("tdqq:nh:add")
    @Log(title = "农户信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TdqqNh tdqqNh)
    {
        return toAjax(tdqqNhService.insertTdqqNh(tdqqNh));
    }

    /**
     * 修改农户信息
     */
    @RequiresPermissions("tdqq:nh:edit")
    @Log(title = "农户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TdqqNh tdqqNh)
    {
        return toAjax(tdqqNhService.updateTdqqNh(tdqqNh));
    }

    /**
     * 删除农户信息
     */
    @RequiresPermissions("tdqq:nh:remove")
    @Log(title = "农户信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{nhIds}")
    public AjaxResult remove(@PathVariable Long[] nhIds)
    {
        return toAjax(tdqqNhService.deleteTdqqNhByNhIds(nhIds));
    }
}
